﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Data.Common;
using St.MODLD;

namespace St.DAL
{
    /// <summary>
    /// 管理员的数据库相关操作
    /// </summary>
    public class AdminService
    {

        static    DBHelper db = null;
        public AdminService()
        {
            db = new SqlServerDBHelper();
        }
        public static string EncryptWithMD5(string source)
        {
            byte[] sor = Encoding.UTF8.GetBytes(source);
            MD5 md5 = MD5.Create();
            byte[] result = md5.ComputeHash(sor);
            StringBuilder strbul = new StringBuilder(40);
            for (int i = 0; i < result.Length; i++)
            {
                strbul.Append(result[i].ToString("x2"));

            }
            return strbul.ToString().ToLower();
        }

        public AdminInfo Login(string name, string pass)
        {
            string sql = string.Format(@"select * from useryonghu
where userName='{0}' and userPass='{1}'", name, EncryptWithMD5(pass));
            Console.WriteLine(sql);

            AdminInfo admin = null;
            DbDataReader dr
            = db.GetReader(sql);
            
            if (dr.Read())
            {
                admin = new AdminInfo();
                admin.AdminId = Convert.ToInt32(dr["userID"]);
                admin.AdminName = dr["userName"].ToString();
                admin.AdminPass = dr["userPass"].ToString();
                admin.State = Convert.ToInt32(dr["State"]);
            }
            dr.Close();
            db.Close();

            return admin;
        }
    }
}
